package edu.westga.vocalengineer.gui;

import java.awt.*;

import java.awt.event.*;
import java.io.File;
import java.util.ArrayList;

import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;

import edu.westga.vocalengineer.audio.*;
import edu.westga.vocalengineer.audio.gui.*;

public class VocalEngineerGUI {

	/**
	 * VocalEngineerGUI provides a graphical user interface allowing the user to
	 * load a wave file and play it back, optionally modified by a number of 
	 * configurable transformations.
	 *
	 * @author Matthew Farmer Nov 9, 2009
	 * 
	 */
	
	private JFrame mainWindow;
	private JPanel theContentPanel;
	
	private JPanel playFilePanel;
	private File selectedFile;
	
	private AudioData theData;

	private ArrayList<IGuiTransform> guiTransforms;
	
	/**
	 * Sets up the GUI and all transform panels.
	 */
	public VocalEngineerGUI() {

		setUpMainWindow();
				
		addPlayFilePanel();
		
		createGuiTransforms();
		addGuiTransformPanels();
		
		mainWindow.setContentPane(this.theContentPanel);
		theContentPanel.validate();
	}
	
	private void addPlayFilePanel() {
		playFilePanel = new JPanel();
		
		JTextField selectedFileName = new JTextField(30);
		selectedFileName.setEditable(false);
		selectedFileName.setText("(no file selected)");
		
		JButton openFileButton = new JButton("Open File...");
		openFileButton.addActionListener(new OpenFileListener(selectedFileName));
		
		JButton playFileButton = new JButton("Play!");
		playFileButton.addActionListener(new PlayFileListener());
		
		JButton stopFileButton = new JButton("Stop");
		stopFileButton.addActionListener(new ActionListener() {
			@Override public void actionPerformed(ActionEvent arg0) {
				theData.requestStop();
			}
		});
				
		playFilePanel.add(openFileButton);
		playFilePanel.add(playFileButton);
		playFilePanel.add(stopFileButton);
		playFilePanel.add(new JLabel("Selected file:"));
		playFilePanel.add(selectedFileName);
		
		playFilePanel.setBorder(BorderFactory.createLineBorder(Color.black));
		
		theContentPanel.add(playFilePanel);
	}

	/**
	 * Displays the frame in the middle of the screen.
	 */
	public void show() {
		this.mainWindow.pack();
		this.mainWindow.setLocationRelativeTo(null);
		this.mainWindow.setVisible(true);
	}
	
	private void setUpMainWindow() {
		this.mainWindow = new JFrame("Vocal Engineer");
		this.mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		int xSize = 700; 
		int ySize = 400;
		this.mainWindow.setPreferredSize(new Dimension(xSize, ySize));
		theContentPanel = new JPanel();
	}
	
	private void addGuiTransformPanels() {
		for(IGuiTransform igt : guiTransforms) {
			theContentPanel.add(igt.getOptionsPanel());
		}
		
	} 

	private void createGuiTransforms() {
		guiTransforms = new ArrayList<IGuiTransform>();
		guiTransforms.add(new VolumeGuiTransform());
		guiTransforms.add(new ReverseGuiTransform());
		guiTransforms.add(new PitchChangeGuiTransform());
		guiTransforms.add(new ChorusGuiTransform());
		guiTransforms.add(new EchoGuiTransform());	
	}

	
	
	// ***************************************************************
	// ****************** Inner listener classes *********************
	// ***************************************************************
	/**
	 * Event handler for an action performed to the play button.
	 */
	private class PlayFileListener implements ActionListener {
		
		@Override
		public void actionPerformed(ActionEvent arg0) {
			if(selectedFile != null){
				theData = new AudioData(selectedFile.getAbsolutePath());
				for(IGuiTransform igt : guiTransforms) {
					theData.applyGuiTransform(igt);
				}
				
				theData.start();
				
			}
			
			else{
				JOptionPane.showMessageDialog(null, "Please select a file to play.");
			}
		}

	}
	
	/**
	 * Event handler for an action performed to the open file button.
	 */
	private class OpenFileListener implements ActionListener
	{
		private JTextField output;
		
		public OpenFileListener(JTextField outputTarget) {
			output = outputTarget;
		}
		
		@Override
		public void actionPerformed(ActionEvent arg0)
		{
			final JFileChooser getFile = new JFileChooser();
			File startFolder = new File(".");
			FileNameExtensionFilter filter = new FileNameExtensionFilter("Wave file (*.wav)", "wav");
			getFile.setCurrentDirectory(startFolder);
			getFile.setFileFilter(filter);
			
			int returnState = getFile.showOpenDialog(null);
	        if (returnState == JFileChooser.APPROVE_OPTION) {
	          selectedFile = getFile.getSelectedFile();	     
	          output.setText(selectedFile.getName());	          
	        }
		}
		
	}
	
}


